package com.sundae.smarthomedemo.Activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.sundae.smarthomedemo.MyAppConfig;
import com.sundae.smarthomedemo.R;
import com.sundae.smarthomedemo.Utils.ToastUtils;

public class LoginActivity extends AppCompatActivity {

    private ImageView look_imageView , pull_imageView;
    private EditText user_tv , pwd_tv;
    private Button back_btn , login_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        SharedPreferences sharedPreferences = getSharedPreferences(MyAppConfig.ShareP_Name , 0);
        sharedPreferences.edit().putString("username" , "").commit();
        if(!MyAppConfig.user_name.equals(""))
        {
            MyAppConfig.user_name = "";
            ToastUtils.showLongToast(getApplicationContext() , "已登出");
        }

        initView();
    }

    private void initView()
    {
        look_imageView = (ImageView) findViewById(R.id.login_look);
        pull_imageView = (ImageView) findViewById(R.id.pull_button);

        user_tv = (EditText) findViewById(R.id.login_user);
        pwd_tv = (EditText) findViewById(R.id.login_pwd);

        back_btn = (Button) findViewById(R.id.login_back);
        login_btn = (Button) findViewById(R.id.login_login_btn);


        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        pull_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShortToast(LoginActivity.this , "咳咳咳～～");
            }
        });

        look_imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    pwd_tv.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    look_imageView.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.look_down));
                    pwd_tv.setSelection(pwd_tv.getText().length());
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_UP)
                {
                    pwd_tv.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    look_imageView.setBackground(ContextCompat.getDrawable(getApplicationContext() , R.drawable.look));
                    pwd_tv.setSelection(pwd_tv.getText().length());
                    return true;
                }
                return false;
            }
        });


        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = user_tv.getText().toString();
                String pwd = pwd_tv.getText().toString();
                if(user == null || user.equals(""))
                {
                    ToastUtils.showShortToast(getApplicationContext() , "用户名不能为空");
                    return;
                }
                if(pwd == null || pwd.equals(""))
                {
                    ToastUtils.showShortToast(getApplicationContext() , "密码不能为空");
                    return;
                }

                if( (user.equals("111") && pwd.equals("111")) || (user.equals("123") && pwd.equals("123")) )
                {
                    SharedPreferences sharedPreferences = getSharedPreferences(MyAppConfig.ShareP_Name , 0);
                    sharedPreferences.edit().putString("username" , user).commit();
                    ToastUtils.showLongToast(getApplicationContext() , "登录成功");
                    startActivity(new Intent(LoginActivity.this , MainActivity.class));
                }else{
                    ToastUtils.showShortToast(getApplicationContext() , "用户名或密码错误");
                }
            }
        });


    }
}
